package com.fingard.dsp.bank.directbank.chinaums01;

import java.util.Date;
import java.util.HashMap;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IQRPay;
import com.fingard.dsp.bank.reqrespobj.qrpay.ReqGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.qrpay.RetAtsGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

public class ChinaUMS01QRPay extends ChinaUMS01Base implements IQRPay {
	@Override
	public RetAtsGenQRInfo genQRInfo(ReqGenQRInfo p_req) {
		RetAtsGenQRInfo retAts = new RetAtsGenQRInfo();
		
		retAts.rdSeq = p_req.rdSeq;
		retAts.respCode = "0";
		retAts.respInfo = reqHeader.bankCode + "无此接口";
		retAts.qrInfo = "";
		
		return retAts;
	}

	@Override
	public RetAtsUnifiedOrder submitQROrder(ReqUnifiedOrder p_req) {
		RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
		retAts.rdSeq = p_req.rdSeq;

		try {
			ActSetItem actItem = getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "unifiedorder");
			if (actItem == null) {
				retAts.respCode = "0";
				retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
				retAts.qrInfo = "";
				return retAts;
			}
			
			HashMap<String, String> mapParams = new HashMap<String, String>();
			
			mapParams.put("msgId", ""); /* 消息ID */
			mapParams.put("msgSrc", getMsgSrc()); /* 消息来源 */
			mapParams.put("msgType", "bills.getQRCode"); /* 消息类型 */
			mapParams.put("requestTimestamp", Format.DateTimeFormat.format(new Date())); /* 报文请求时间 (格式:yyyy-MM-dd HH:mm:ss)*/
			mapParams.put("srcReserve", ""); /* 请求系统预留字段 */
			mapParams.put("mid", actItem.merchantId); /* 商户号 */
			mapParams.put("tid", getTerminalNum()); /* 终端号 */
			mapParams.put("instMid", "QRPAYDEFAULT"); /* 业务类型 */
			mapParams.put("billNo", p_req.rdSeq); /* 账单号 */
			mapParams.put("billDate", Format.Date10Format.format(p_req.getTimeStart())); /* 账单日期 (格式yyyy-MM-dd) */
			mapParams.put("billDesc", p_req.description); /* 账单描述 */
			mapParams.put("totalAmount", StringHelper.amountMultiply100(p_req.amount)); /* 支付总金额 */

			// Attach: 账号#线路#指令。 现转为：counterNo存线路+指令，memberId存账号
			String attach = p_req.attach;
			String memberId = attach.substring(0, attach.indexOf("#"));
			String counterNo = attach.substring(attach.indexOf("#")+1);
				
			mapParams.put("memberId", memberId); /* 会员号 */
			mapParams.put("counterNo", counterNo); /* 桌号 */
			mapParams.put("expireTime", Format.DateTimeFormat.format(p_req.getTimeExpire())); /* 账单过期时间 (格式yyyy-MM-dd HH:mm:ss) */
			mapParams.put("notifyUrl", actItem.notifyURL); /* 支付结果通知地址 */
			mapParams.put("returnUrl", actItem.returnURL); /* 网页跳转地址 */
			mapParams.put("qrCodeId", ""); /* 二维码ID */
			mapParams.put("systemId", ""); /* 系统ID */
			mapParams.put("secureTransaction", "false"); /* 担保交易标识 */
			mapParams.put("walletOption", "MULTIPLE"); /* 钱包选项 */
			mapParams.put("signType", "MD5"); /* 签名算法 */
			
			// 实名认证信息
			mapParams.put("name", p_req.oppActName); /* 实名姓名 */
			mapParams.put("mobile", p_req.cellPhone); /* 实名手机号 */
			mapParams.put("certType", getBankConfig().getMapValueByKey("certTypeQRPayMapping", p_req.certType)); /* 证件类型 */  
			mapParams.put("certNo", p_req.certNumber); /* 实名证件号 */
			if (StringHelper.hasAnyChar(p_req.oppActName) || StringHelper.hasAnyChar(p_req.certNumber) || StringHelper.hasAnyChar(p_req.cellPhone)) {
				mapParams.put("fixBuyer", "T"); /* 是否实名认证 */
			} else {
				mapParams.put("fixBuyer", "F"); 
			}
			
			String tmpStrRet = sendUnifiedOrder(mapParams, actItem);
            
            if (StringHelper.hasAnyChar(tmpStrRet)) {
                if (tmpStrRet.contains("{")) {
                	JSONObject tmpJsonResp = JSON.parseObject(tmpStrRet);
                	String errCode = tmpJsonResp.getString("errCode");
                    if ("SUCCESS".equals(errCode)) {
                    	retAts.respCode = "0";
						retAts.respInfo = tmpJsonResp.getString("errMsg");
						retAts.qrInfo = tmpJsonResp.getString("billQRCode");
                    } else {
                    	retAts.respCode = "-1";
						retAts.respInfo = tmpJsonResp.getString("errMsg");
                    }
                } else {
                	retAts.respCode = "-1";
                	retAts.respInfo = "银联响应数据格式非法";
                }
            } else {
            	retAts.respCode = "-1";
            	retAts.respInfo = "银联响应为空";
            }
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retAts;
	}

	@Override
	public RetAtsQueryOrder queryQRPay(ReqQueryOrder p_req) {
		RetAtsQueryOrder retAts = new RetAtsQueryOrder();
		retAts.rdSeq = p_req.rdSeq;

		try {
			ActSetItem actItem = this.getBankConfig().getActSetItemByAny(p_req.ownAct, p_req.merchantId, p_req.appId, "unifiedorder");
			if (actItem == null) {
				retAts.respCode = "-1";
				retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息";
				return retAts;
			}

			HashMap<String, String> mapParams = new HashMap<String, String>();

			mapParams.put("msgId", ""); /* 消息ID */
			mapParams.put("msgSrc", getMsgSrc()); /* 消息来源 */
			mapParams.put("msgType", "bills.query"); /* 消息类型 */
			mapParams.put("requestTimestamp", Format.DateTimeFormat.format(p_req.getBgnDateTime())); /* 报文请求时间，格式yyyy-MM-dd HH:mm:ss */
			mapParams.put("srcReserve", ""); /* 请求系统预留字段 */
			mapParams.put("mid", actItem.merchantId); /* 商户号 */
			mapParams.put("tid", getTerminalNum()); /* 终端号 */
			mapParams.put("instMid", "QRPAYDEFAULT"); /* 业务类型 */
			mapParams.put("billNo", p_req.rdSeq); /* 账单号 */
			mapParams.put("billDate", Format.Date10Format.format(p_req.getBgnDateTime())); /* 订单时间，格式yyyy-MM-dd */
			mapParams.put("signType", "MD5"); /* 签名算法 */
			
			String tmpStrRet = sendUnifiedOrder(mapParams, actItem);
            
            if (StringHelper.hasAnyChar(tmpStrRet)) {
                if (tmpStrRet.contains("{")) {
                	JSONObject tmpJsonResp = JSON.parseObject(tmpStrRet);
                	
                	retAts.respCode = "0";
        			retAts.respInfo = tmpJsonResp.getString("errMsg");
        			
        			String errCode = tmpJsonResp.getString("errCode");
        			retAts.transState = StringHelper.joinNotEmpty("+", errCode, tmpJsonResp.getString("billStatus"));
    				retAts.payInfoCode = StringHelper.getFirstNotEmptyTrim(tmpJsonResp.getString("billStatus"),errCode);
                } else {
                	retAts.respCode = "-1";
                	retAts.respInfo = "银联响应数据格式非法";
                }
            } else {
            	retAts.respCode = "-1";
            	retAts.respInfo = "银联响应为空";
            }
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.respCode = "-1";
			retAts.respInfo = ex.getMessage();
			WriteBankLogLn(ex);
		}

		return retAts;
	}
}
